using System;
using System.Text;
using System.Xml;
using System.Data;
using MyClever.Lib;

namespace MyClever.DefaultPlugins
{
    public class WriteRssFeed : MissionPlugin
    {
        [Package(Direction.Input, "Defines the rss-feed channel setting.")]
        enum RSSFeedChannel
        {
            [Column(typeof(string), "Headline of the feed.")]
            Title,
            [Column(typeof(string), "Link to the website of this feed.",
                Validation="isUrl")]
            Link,
            [Column(typeof(string), "The article/text itself.")]
            Description
        }
        
        [Package(Direction.Input, "Defines the item data which should written in rss-feed format.")]
        enum RSSFeedItem
        {
            [Column(typeof(string), "Headline of the feed item.")]
            Title,
            [Column(typeof(string), "Link to the website of this feed item.")]
            Link,
            [Column(typeof(string), "The item article/text itself.")]
            Description
        }

        [Package(Direction.Input, "Defines the place where do you like to save the rss-file.")]
        enum RSSFeedFile
        {
            [Column(typeof(string), "Path of the rss-feed file.",
               Validation = "IsFile")]
            Filename
        }

        [Plugin(PluginDevStatus.Production, "Create RSS feed programatically from the given data.")]
        public WriteRssFeed()
        {
        }

        public override void Initialize()
        {
            
            //This is the first Function called by the host...
            //Put anything needed to start with here first
        }

        public override void Dispose()
        {
            //Put any cleanup code in here for when the program is stopped
        }

        /// <summary>
        /// Generate new Source-content.
        /// </summary>
        protected override void Work()
        {
            DataTableReader rssFeedFiles = this.GetDataReader(typeof(RSSFeedFile));
            while (rssFeedFiles.Read())
            {
                this.AddNewLoggingMessage(typeof(RSSFeedFile), String.Format("write feed to: {0}", rssFeedFiles[RSSFeedFile.Filename.ToString()].ToString()));

                XmlTextWriter feedWriter = new XmlTextWriter(rssFeedFiles[RSSFeedFile.Filename.ToString()].ToString(), Encoding.UTF8);
                feedWriter.Formatting = Formatting.Indented;

                feedWriter.WriteStartDocument();
                feedWriter.WriteStartElement("rss");
                feedWriter.WriteAttributeString("version", "2.0");

                DataTableReader rssFeedChannel = this.GetDataReader(typeof(RSSFeedChannel));
                rssFeedChannel.Read();
                feedWriter.WriteStartElement("channel");
                feedWriter.WriteElementString("title", rssFeedChannel[RSSFeedChannel.Title.ToString()].ToString());
                feedWriter.WriteElementString("link", rssFeedChannel[RSSFeedChannel.Link.ToString()].ToString());
                feedWriter.WriteElementString("description", rssFeedChannel[RSSFeedChannel.Description.ToString()].ToString());
                rssFeedChannel.Close();

                DataTableReader rssFeedItems = this.GetDataReader(typeof(RSSFeedItem));
                while (rssFeedItems.Read())
                {
                    this.AddNewLoggingMessage(typeof(RSSFeedItem), String.Format("write new feed-item with title: {0}", rssFeedItems[RSSFeedItem.Title.ToString()].ToString()));

                    feedWriter.WriteStartElement("item");
                    feedWriter.WriteElementString("title", rssFeedItems[RSSFeedItem.Title.ToString()].ToString());
                    feedWriter.WriteElementString("description", rssFeedItems[RSSFeedItem.Description.ToString()].ToString());
                    feedWriter.WriteElementString("link", rssFeedItems[RSSFeedItem.Link.ToString()].ToString());
                    feedWriter.WriteElementString("pubDate", System.DateTime.Now.ToString("r"));
                    feedWriter.WriteEndElement();
                }
                
                // close all open tags tags
                //feedWriter.WriteEndElement();
                feedWriter.WriteEndElement();
                feedWriter.WriteEndDocument();
                feedWriter.Flush();
                feedWriter.Close();
            }     
        }
    }
}
